package top.wu2020.lazy.ws.chat.application.impl;

import org.wu.framework.database.lazy.web.plus.stereotype.LazyApplication;
import top.wu2020.lazy.ws.chat.application.ContentApplication;
import org.springframework.web.bind.annotation.*;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import top.wu2020.lazy.ws.chat.domain.model.content.Content;
import top.wu2020.lazy.ws.chat.application.command.content.ContentRemoveCommand;
import top.wu2020.lazy.ws.chat.application.command.content.ContentStoryCommand;
import top.wu2020.lazy.ws.chat.application.command.content.ContentUpdateCommand;
import top.wu2020.lazy.ws.chat.application.command.content.ContentQueryListCommand;
import top.wu2020.lazy.ws.chat.application.command.content.ContentQueryOneCommand;
import top.wu2020.lazy.ws.chat.application.assembler.ContentDTOAssembler;
import top.wu2020.lazy.ws.chat.application.dto.ContentDTO;
import java.util.stream.Collectors;
import jakarta.annotation.Resource;
import top.wu2020.lazy.ws.chat.domain.model.content.ContentRepository;
import java.util.List;
import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;
/**
 * describe content 
 *
 * @author Jia wei Wu
 * @date 2024/05/24 08:52 晚上
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyApplicationImpl 
 **/
@LazyApplication
public class ContentApplicationImpl implements ContentApplication {

    @Resource
    ContentRepository contentRepository;
    /**
     * describe 新增
     *
     * @param contentStoryCommand 新增     
     * @return {@link Result<Content>} 新增后领域对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<Content> story(ContentStoryCommand contentStoryCommand) {
        Content content = ContentDTOAssembler.INSTANCE.toContent(contentStoryCommand);
        return contentRepository.story(content);
    }
    /**
     * describe 批量新增
     *
     * @param contentStoryCommandList 批量新增     
     * @return {@link Result<List<Content>>} 新增后领域对象集合     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<List<Content>> batchStory(List<ContentStoryCommand> contentStoryCommandList) {
        List<Content> contentList = contentStoryCommandList.stream().map( ContentDTOAssembler.INSTANCE::toContent).collect(Collectors.toList());
        return contentRepository.batchStory(contentList);
    }
    /**
     * describe 更新
     *
     * @param contentUpdateCommand 更新     
     * @return {@link Result<Content>} 领域对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<Content> updateOne(ContentUpdateCommand contentUpdateCommand) {
        Content content = ContentDTOAssembler.INSTANCE.toContent(contentUpdateCommand);
        return contentRepository.story(content);
    }

    /**
     * describe 查询单个
     *
     * @param contentQueryOneCommand 查询单个     
     * @return {@link Result<ContentDTO>} DTO对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<ContentDTO> findOne(ContentQueryOneCommand contentQueryOneCommand) {
        Content content = ContentDTOAssembler.INSTANCE.toContent(contentQueryOneCommand);
        return contentRepository.findOne(content).convert(ContentDTOAssembler.INSTANCE::fromContent);
    }

    /**
     * describe 查询多个
     *
     * @param contentQueryListCommand 查询多个     
     * @return {@link Result<List<ContentDTO>>} DTO对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<List<ContentDTO>> findList(ContentQueryListCommand contentQueryListCommand) {
        Content content = ContentDTOAssembler.INSTANCE.toContent(contentQueryListCommand);
        return contentRepository.findList(content)        .convert(contents -> contents.stream().map(ContentDTOAssembler.INSTANCE::fromContent).collect(Collectors.toList())) ;
    }

    /**
     * describe 分页查询多个
     *
     * @param contentQueryListCommand 分页查询多个     
     * @return {@link Result<LazyPage<ContentDTO>>} 分页DTO对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<LazyPage<ContentDTO>> findPage(int size,int current,ContentQueryListCommand contentQueryListCommand) {
        Content content = ContentDTOAssembler.INSTANCE.toContent(contentQueryListCommand);
        return contentRepository.findPage(size,current,content)        .convert(page -> page.convert(ContentDTOAssembler.INSTANCE::fromContent))            ;
    }

    /**
     * describe 删除
     *
     * @param contentRemoveCommand 删除     
     * @return {@link Result<Content>}      
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<Content> remove(ContentRemoveCommand contentRemoveCommand) {
     Content content = ContentDTOAssembler.INSTANCE.toContent(contentRemoveCommand);
     return contentRepository.remove(content);
    }

}